/*
 *Create By：Z.B
 *@DATE 2019/3/19 17:27
 *@EMAIL: 2545375@qq.com
 */
'use strict';

const Controller = require('egg').Controller;

class RoleController extends Controller{

  constructor(ctx) {
    super(ctx);

    this.createRule = {
      name: { type: 'string', required: true, allowEmpty: false },
      access: { type: 'string', required: true, allowEmpty: false }
    }
  }

  async create(){
    const {ctx, service} = this;
    ctx.validate(this.createRule);

    const payload = ctx.request.body || {};

    const res = await service.role.create(payload);

    ctx.hepler.success({ctx,res});
  }

  async destroy(){
    const {ctx, service} = this;

    const {id} = ctx.params;
    await service.role.destroy(id);

    ctx.helper.success({ctx});
  }

  //修改角色
  async update(){
    const {ctx, service} = this;
    ctx.validate(this.createRule);

    //组装参数
    const {id} = ctx.params;
    const payload = ctx.request.body || {};
    await service.role.update(id, payload);
    //设置响应参数
    ctx.helper.success({ctx});
  }

  //获取单个角色
  async show(){
    const { ctx, service} = this;

    //获取参数
    const {id} = ctx.params;
    //获取角色
    await service.role.show(id);

    ctx.helper.success({ctx});
  }

  //删除所有角色
  async removes(){
    const {ctx, service} = this;
    //组装参数
    const {id} = ctx.request.body;
    const payload = id.split(',') || [];
    const result = await service.role.removes(payload);
    ctx.helper.success({ctx});
  }
}